package com.fengze.business.service.impl;

import java.time.LocalDate;
import java.util.List;

import com.fengze.business.mapper.ProjectInfoMapper;
import com.fengze.common.annotation.AutoSetInsertValue;
import com.fengze.common.annotation.AutoSetUpdateValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fengze.business.mapper.ProjectPlanMapper;
import com.fengze.business.domain.ProjectPlan;
import com.fengze.business.service.IProjectPlanService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 项目计划Service业务层处理
 * 
 * @author lug
 * @date 2024-02-02
 */
@Service
public class ProjectPlanServiceImpl implements IProjectPlanService 
{
    @Resource
    private ProjectPlanMapper projectPlanMapper;

    @Resource
    private ProjectInfoMapper projectInfoMapper;

    /**
     * 查询项目计划
     * 
     * @param id 项目计划主键
     * @return 项目计划
     */
    @Override
    public ProjectPlan selectProjectPlanById(Long id)
    {
        return projectPlanMapper.selectProjectPlanById(id);
    }

    /**
     * 查询项目计划列表
     * 
     * @param projectPlan 项目计划
     * @return 项目计划
     */
    @Override
    public List<ProjectPlan> selectProjectPlanList(ProjectPlan projectPlan)
    {
        return projectPlanMapper.selectProjectPlanList(projectPlan);
    }

    /**
     * 新增项目计划
     * 
     * @param projectPlan 项目计划
     * @return 结果
     */

    @Transactional(rollbackFor = Exception.class)
    @AutoSetInsertValue
    @Override
    public int insertProjectPlan(ProjectPlan projectPlan)
    {
        int ret =  projectPlanMapper.insertProjectPlan(projectPlan);
        long projectId = projectPlan.getProjectId();
        int status = projectPlan.getStatus();
        LocalDate today = LocalDate.now();
        int count = projectPlanMapper.countByCondition(projectId,status,today.toString());
        if(count > 0){
            int newStatus =  1;
            int oldStatus = 0;
            projectInfoMapper.updateStatusById(projectId,newStatus,oldStatus);
        }else{
            int newStatus = 0;
            int oldStatus = 1;
            projectInfoMapper.updateStatusById(projectId,newStatus,oldStatus);
        }

        return ret;
    }

    /**
     * 修改项目计划
     * 
     * @param projectPlan 项目计划
     * @return 结果
     */

    @Transactional(rollbackFor = Exception.class)
    @AutoSetUpdateValue
    @Override
    public int updateProjectPlan(ProjectPlan projectPlan)
    {
        int ret =  projectPlanMapper.updateProjectPlan(projectPlan);

        LocalDate today = LocalDate.now();
        int status = 0;
        Long projectId = projectPlan.getProjectId();
        int count = projectPlanMapper.countByCondition(projectId,status,today.toString());
        if(count > 0){
            int newStatus =  1;
            int oldStatus = 0;
            projectInfoMapper.updateStatusById(projectId,newStatus,oldStatus);
        }else{
            int newStatus = 0;
            int oldStatus = 1;
            projectInfoMapper.updateStatusById(projectId,newStatus,oldStatus);
        }
        return ret;
    }

    /**
     * 批量删除项目计划
     * 
     * @param ids 需要删除的项目计划主键
     * @return 结果
     */
    @Override
    public int deleteProjectPlanByIds(Long[] ids)
    {
        return projectPlanMapper.deleteProjectPlanByIds(ids);
    }

    /**
     * 删除项目计划信息
     * 
     * @param id 项目计划主键
     * @return 结果
     */
    @Override
    public int deleteProjectPlanById(Long id)
    {
        return projectPlanMapper.deleteProjectPlanById(id);
    }
}
